package com.java.boot.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.boot.admin.bean.City;
import com.java.boot.admin.bean.Department;
import com.java.boot.admin.bean.User;
import com.java.boot.admin.service.CityService;
import com.java.boot.admin.service.DepartmentService;
import com.java.boot.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author jingjintao
 * @date 2021/7/27 - 0:29
 **/
@Controller
public class TableController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private CityService cityService;
    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping("/city")
    public City getCity(@RequestParam("id") Long id){
        return cityService.getCityById(id);
    }

    @ResponseBody
    @PostMapping("/city")
    public City saveCity(City city){
        cityService.saveCity(city);
        return city;
    }


    @ResponseBody
    @GetMapping("/dept")
    public Department getDept(@RequestParam("id") Integer id){
        return departmentService.getDept(id);
    }

    @GetMapping("/sql")
    @ResponseBody
    public String sql(){
        Long count = jdbcTemplate.queryForObject("select count(*) from tbl_dept", Long.class);
        return count.toString();
    }
    /**
     *
     * @param a  不带请求参数或者参数类型不对  400；Bad Request  一般都是浏览器的参数没有传递正确
     * @return
     */
    @GetMapping("/basic_table")
    public String basic_table(@RequestParam("a") Integer a){
        int i = 10/0;
        return "table/basic_table";
    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                             RedirectAttributes ra){

        //RedirectAttributes ra  重定向携带参数的
        userService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value = "pn",defaultValue = "1") Integer pn
            ,Model model){

//        List<User> users = Arrays.asList(new User("zhangsan", "123456"),
//                new User("lisi", "qwertasd"),
//                new User("admin", "admin123"),
//                new User("test", "666666"));
//        if(users.size()>3){
//            throw new UserTooManyException();
//        }
//        model.addAttribute("users",users);

        //从数据库中查出user表中的用户进行展示
        //构造分页参数
        Page<User> page = new Page<>(pn,2);
        //调用page进行分页
        Page<User> userPage = userService.page(page);

//        userPage.getTotal();
//        userPage.getRecords();
//        userPage.getCurrent();
//        userPage.getPages();
        model.addAttribute("userPage",userPage);
        return "table/dynamic_table";
    }
}
